﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using Logistics.DataManage;
using Logistics.DataManage.Local;
using Logistics.DataManage.DAL;

namespace Logistics.Print
{
    class PrintContext
    {
        private string BillId;
        public PrintContext(String _billId)
        {
            if (String.IsNullOrWhiteSpace(_billId))
                throw new ArgumentNullException("_billId is empty");
            this.BillId = _billId;
        }

        public Hashtable GetHashContext()
        {
            if (PrinterUtils.PRINTER_TEST_BILL_ID == BillId)
            {
                string SQL = "SELECT * FROM WaybillInfo LIMIT 1 ";
                DataSet Bill = LocalDb.Handler.ExecuteQuery(SQL);
                if (Bill == null || Bill.Tables[0].Rows.Count == 0)
                    throw new Exception("没有任何数据可供测试，请确保数据库中至少有一条运单数据!");
                return FillPrintContext(Bill.Tables[0]);
            }
            else
            {
                DataSet Bill = WaybillDAO.GetBillSetById(BillId);
                if (Bill == null || Bill.Tables[0].Rows.Count == 0)
                    throw new ArgumentException("无法找打运单，单号：" + BillId);

                return FillPrintContext(Bill.Tables[0]);
            }
        }

        //private Hashtable FillFakePrintContext()
        //{
        //    return new Hashtable();
        //}

        private Hashtable FillPrintContext(DataTable Bill)
        {
            Hashtable PrintContext = new Hashtable();
            DataRow row = Bill.Rows[0];
            foreach (DataColumn col in Bill.Columns)
            {
                String name = col.ColumnName.ToLower();
                Object val = row[name];
                PrintContext.Add(name, val);
            }
            FillText(PrintContext);
            return PrintContext;
        }

        private void FillText(Hashtable hash)
        {
            string Origin = Convert.ToString(hash["origin"]);//发送站点
            string OriginNetWork = Convert.ToString(hash["originnetwork"]);//发送网点
            string Destinations = Convert.ToString(hash["destinations"]);//接收站点
            string DestinationsNetwork = Convert.ToString(hash["destinationsnetwork"]);//接收网点

            if (!String.IsNullOrWhiteSpace(Origin))
            {
                String name = SitesDAO.GetSiteName(Origin);
                hash["origin.text"] = name;
            }
            if (!String.IsNullOrWhiteSpace(OriginNetWork))
            {
                String name = OutletsDAO.GetOutletsNameById(OriginNetWork);
                hash["originnetwork.text"] = name;
            }
            if (!String.IsNullOrWhiteSpace(Destinations))
            {
                String name = SitesDAO.GetSiteName(Destinations);
                hash["destinations.text"] = name;
            }
            if (!String.IsNullOrWhiteSpace(DestinationsNetwork))
            {
                String name = OutletsDAO.GetOutletsNameById(DestinationsNetwork);
                hash["destinationsnetwork.text"] = name;
            }

            //制票人
            string CreateUser = Convert.ToString(hash["createuser"]);
            if (!String.IsNullOrWhiteSpace(CreateUser))
            {
                String name = UserDAO.GetUserRealName(CreateUser);
                hash["createuser.text"] = name;
            }

            //收货员工
            string Column3 = Convert.ToString(hash["column3"]);
            if (!String.IsNullOrWhiteSpace(CreateUser))
            {
                String name = UserDAO.GetUserRealName(Column3);
                hash["column3.text"] = name;
            }

            //电话：默认填手机号，如果手机号为空则填座机
            string ConsigneeTel = Convert.ToString(hash["consigneetel"]);
            string ConsigneeMobile = Convert.ToString(hash["consigneemobile"]);
            hash["consigneemobile_consigneetel"] = String.IsNullOrWhiteSpace(ConsigneeMobile) ? ConsigneeTel : ConsigneeMobile;

            //电话：默认填手机号，如果手机号为空则填座机
            string ConsignorTel = Convert.ToString(hash["consignortel"]);
            string ConsignorMobile = Convert.ToString(hash["consignormobile"]);
            hash["consignormobile_consignortel"] = String.IsNullOrWhiteSpace(ConsignorMobile) ? ConsignorTel : ConsignorMobile;

            //费用合计：始发站收+目的站收
            int OriginAccept = Convert.ToInt32(hash["originaccept"]);
            int DestinationAccept = Convert.ToInt32(hash["destinationaccept"]);
            hash["originaccept_destinationaccept"] = OriginAccept + DestinationAccept;

            //线路
            ORoute route = RouteDAO.GetRouteByStartEndId(Origin, Destinations);
            string routeName = route == null ? "未知" : route.RouteName;
            hash["routename"] = routeName;
            //线路2
            hash["routename2"] = Convert.ToString(hash["originnetwork.text"]) + "——>" + Convert.ToString(hash["destinations.text"]);

            //支付方式
            String PayMode = Convert.ToString(hash["paymode"]);
            String PayModeText = PayMode == "1" ? "提付" : "现付";
            hash["paymode.text"] = PayModeText;

            //运费，如果 付款方式 是“提付”，则 运费一栏中应该显示：xx（欠）；否则显示 xx(清)
            string Freight = Convert.ToString(hash["freight"]);
            string FreightText = PayMode == "1" ? Freight + "(欠)" : Freight + "(清)";
            hash["freight.text"] = FreightText;

            //如果 保价费 是“源站点收”，则 保价费一栏中应显示 xx（清），如果显示 xx（欠）
            String Column1 = Convert.ToString(hash["column1"]);
            String InsuredServicesFee = Convert.ToString(hash["insuredservicesfee"]);
            String InsuredServicesFeeText = Column1 == "1" ? InsuredServicesFee + "(清)" : InsuredServicesFee + "(欠)";
            hash["insuredservicesfee.text"] = InsuredServicesFeeText;

            //如果 送货费 是“源站点收”，则 送货费一栏中应显示 xx（清），否则显示 xx（欠）
            String Column2 = Convert.ToString(hash["column2"]);
            String DeliveryCharges = Convert.ToString(hash["deliverycharges"]);
            String DeliveryChargesText = Column2 == "1" ? DeliveryCharges + "(清)" : DeliveryCharges + "(欠)";
            hash["deliverycharges.text"] = DeliveryChargesText;

            //如果有代收款有卡号的 需要填“自动转账”；有代收款无卡号需要填“转账登记”；无代收款的不用填
            String CollectionFee = Convert.ToString(hash["collectionfee"]);
            if(!String.IsNullOrWhiteSpace(CollectionFee) && CollectionFee!="0")
            {
                String text = "";
                String ConsignorCardID = Convert.ToString(hash["consignorcardid"]);
                if (String.IsNullOrWhiteSpace(ConsignorCardID))
                    text = "转账登记";
                else
                    text = "自动转账";
                hash["collectionfee.text"] = text;
            }

            //打印的时候，在货号后面跟“-件数”，例如货号是 01贝韩00001，件数为2，则打印出来的应为01贝韩00001-2
            String GoodsCode = Convert.ToString(hash["goodscode"]);
            String numbers = Convert.ToString(hash["numbers"]);
            hash["goodscode_numbers"] = GoodsCode + "-" + numbers;
        }
    }
}
